package com.turing.data.engine.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.turing.common.entity.TextVO;
import com.turing.common.util.Log;
import com.turing.data.engine.TextUtils;
import com.turing.data.vo.out.review.DependParamsVO;
import com.turing.data.vo.out.review.TaskElementVO;
import org.apache.commons.lang.SerializationUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexExtract {

    public static void doRuleExtract(String regex, String childTaskSn, DependParamsVO dependParams, String jsonText, List<TaskElementVO.DrawResultVO> trueValueSet) {
        if (CollectionUtils.isEmpty(trueValueSet)) {
            TaskElementVO.DrawResultVO initDrawResultVO = new TaskElementVO.DrawResultVO();
            initDrawResultVO.setChildTaskSn(childTaskSn);

            StringBuilder cleanText = new StringBuilder();
            List<TextVO> textVOS = JSON.parseObject(jsonText, new TypeReference<ArrayList<TextVO>>() {
            });
            for (TextVO textVO : textVOS) {
                List<TextVO.Line> lines = textVO.getParagraphs().get(0).getLines();
                for (TextVO.Line line : lines) {
                    if (line.getObjType().equals("textLine")) {
                        cleanText.append(line.getObjContent());
                    } else {
                        String objContent = line.getObjContent();
                        cleanText.append(objContent.replaceAll(":",""));
                    }
                }
            }

            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(cleanText.toString());
            List<MatchedGroup> matchedGroupList = new ArrayList<>();
            MatchedGroup matchedGroup;
            while (matcher.find()) {
                Log.info(">>>>>>>>>>>> matcher found..." + matcher.group());
                matchedGroup = new MatchedGroup();
                matchedGroup.setSonOfStr(matcher.group());//子串内容
                matchedGroup.setIndexStart(matcher.start());//子串在全文中的开始位置
                matchedGroup.setIndexEnd(matcher.end());//子串在全文中的结束位置
                matchedGroupList.add(matchedGroup);
            }

            Log.info(">>>>>>>>>> matched group is " + matchedGroupList);
            if (CollectionUtils.isEmpty(matchedGroupList)) {
                return;
            }

            matchedGroup = Collections.min(matchedGroupList);
            initDrawResultVO.setStart((int) matchedGroup.getIndexStart());
            initDrawResultVO.setEnd((int) matchedGroup.getIndexEnd());
            Log.info(">>>>>>> init Draw Result VO " + initDrawResultVO.toString());
            Log.info(">>>>>>> initDraw start " + initDrawResultVO.getStart() + ", end " + initDrawResultVO.getEnd());
            Log.info(">>>>>>> Matched start " + matchedGroup.getIndexStart() + ", end " + matchedGroup.getIndexEnd());
            TaskElementVO.DrawResultVO ObjectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(initDrawResultVO);
            ObjectNew = TextUtils.correctPosInfoByIndex(ObjectNew, initDrawResultVO.getStart(), initDrawResultVO.getEnd(), jsonText, matchedGroup.getSonOfStr());
            trueValueSet.add(ObjectNew);
        }
    }

}
